package com.visual.plate.app;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@RequestMapping("/plate")
public class PlateController {
    @Value("${plate.recognition.serverUrl}")
    private String plateRecognitionUrl;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/recognition")
    @ResponseBody
    public Object recognition(@RequestBody JSONObject body){
        String imageBase64 = body.getString("imageBase64");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject personJsonObject = new JSONObject();
        personJsonObject.put("image", imageBase64);
        personJsonObject.put("limit", 20);
        HttpEntity<String> request = new HttpEntity<>(personJsonObject.toString(), headers);
        return restTemplate.postForObject(plateRecognitionUrl, request, String.class);
    }

}
